package com.bootdo.act.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.bootdo.act.domain.ActTypeDO;
import com.bootdo.act.service.ActTypeService;
import com.bootdo.common.config.Constant;
import com.bootdo.common.domain.Tree;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.system.domain.DeptDO;

/**
 * 项目类别
 * 
 * @author kewenxiu
 * @email 904358838@qq.com
 * @date 2017-11-23 09:47:38
 */
 
@Controller
@RequestMapping("/act/actType")
public class ActTypeController {
	@Autowired
	private ActTypeService actTypeService;
	
	@GetMapping()
	@RequiresPermissions("act:actType:actType")
	String ActType(){
	    return "act/actType/actType";
	}
	
	@ResponseBody
	@GetMapping("/get/{id}")
	public ActTypeDO getById(@PathVariable("id") Long id){
		System.out.println(id);
		return actTypeService.get(id);
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("act:actType:actType")
	public List<ActTypeDO> list(String name){
		Map<String, Object> query = new HashMap<>(16);
		if(!StringUtils.isEmpty(name)){
			query.put("name", name);
		}
		List<ActTypeDO> list = actTypeService.list(query);
		return list;
	}
	
	@GetMapping("/add/{pId}")
	@RequiresPermissions("act:actType:add")
	String add(@PathVariable("pId") Long pId, Model model) {
		model.addAttribute("pId", pId);
		if (pId == 0) {
			model.addAttribute("pName", "顶级分类");
		} else {
			model.addAttribute("pName", actTypeService.get(pId).getName());
		}
		return  "act/actType/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("act:actType:edit")
	String edit(@PathVariable("id") Long id,Model model){
		ActTypeDO actType = actTypeService.get(id);
		model.addAttribute("actType", actType);
		if(Constant.DEPT_ROOT_ID.equals(actType.getParentId())) {
			model.addAttribute("parentActName", "无");
		}else {
			ActTypeDO parAct = actTypeService.get(actType.getParentId());
			model.addAttribute("parentActName", parAct.getName());
		}
	    return "act/actType/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("act:actType:add")
	public R save( ActTypeDO actType){
		if(actTypeService.save(actType)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("act:actType:edit")
	public R update( ActTypeDO actType){
		actTypeService.update(actType);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("act:actType:remove")
	public R remove( Long id){
		if(actTypeService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("act:actType:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		actTypeService.batchRemove(ids);
		return R.ok();
	}
	
	@GetMapping("/treeView")
	String treeView() {
		return  "act/actType/typeTree";
	}
	
	@GetMapping("/tree")
	@ResponseBody
	public Tree<ActTypeDO> tree() {
		Tree<ActTypeDO> tree = new Tree<ActTypeDO>();
		tree = actTypeService.getTree();
		return tree;
	}
}
